import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/components/button_components/base_drawer_bottom.dart';
import 'package:x3_component_core/components/selector_components/advanced_filter/base_filter_data_provider.dart';
import 'package:x3_component_core/components/selector_components/advanced_filter/base_filter_input_date_range.dart';
import 'package:x3_component_core/components/selector_components/advanced_filter/base_filter_input_range.dart';
import 'package:x3_component_core/components/selector_components/advanced_filter/base_filter_multiple.dart';
import 'package:x3_component_core/components/selector_components/advanced_filter/base_filter_radio.dart';
import 'package:x3_component_core/models/base_advanced_filter_model.dart';

class BaseAdvancedFilterDrawer extends StatefulWidget {
  final BaseFilterDataProvider filterDataProvider;

  final Function(Map<String, dynamic>) onSure;
  const BaseAdvancedFilterDrawer({Key key, @required this.onSure, @required this.filterDataProvider})
      : assert(onSure != null),
        assert(filterDataProvider != null),
        super(key: key);
  @override
  _BaseAdvancedFilterDrawerState createState() => _BaseAdvancedFilterDrawerState();
}

class _BaseAdvancedFilterDrawerState extends State<BaseAdvancedFilterDrawer> with AutomaticKeepAliveClientMixin {
  BaseFilterDataProvider get _model => widget.filterDataProvider;
  Map<String, dynamic> _dataSource;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    _dataSource = Map.from(_model.dataSource);
    assert(_model != null);
    return SafeArea(
      child: Column(
        children: [
          Expanded(
            child: ListView.separated(
              itemBuilder: _buildFilterItem,
              separatorBuilder: (_, __) => SizedBox(),
              itemCount: this._model.models.length,
            ),
          ),
          _buildFooterButton(),
        ],
      ),
    );
  }

  Widget _buildFooterButton() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      child: BaseDrawerBottom(
        sure: () {
          widget.onSure(_dataSource);
          _model.dataSource = _dataSource;
          Navigator.pop(context);
        },
        close: () => {Navigator.pop(context)},
      ),
    );
  }

  Widget _buildFilterItem(BuildContext context, int index) {
    Widget child;
    AdvancedFilterModel item = this._model.models[index];
    AdvancedFilterType type = BaseTransformUtil.enumFromString(AdvancedFilterType.values, item.tagType);
    switch (type) {
      case AdvancedFilterType.Radio:
        child = BaseFilterRadio(dataSource: _dataSource, model: item);
        break;
      case AdvancedFilterType.MultipleCheckbox:
        child = BaseFilterMultiple(dataSource: _dataSource, model: item);
        break;
      case AdvancedFilterType.InputDateRange:
        child = BaseFilterInputDateRange(
          dataSource: _dataSource,
          model: item,
          dateRangeList: this._model.dateRangeList,
        );
        break;
      case AdvancedFilterType.InputRange:
        child = BaseFilterInputRange(dataSource: _dataSource, model: item);
        break;
    }
    return child ?? SizedBox();
  }

  @override
  bool get wantKeepAlive => true;
}
